package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.enums.ReservationStatus;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.ReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationRestCountVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @Description CustomerReservationServiceImpl
 * @Author loe
 * @Date 2024-07-12
 */

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {

    @Autowired
    private ReservationMapper reservationMapper;

    @Override
    public Integer getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        LocalDateTime now = LocalDateTime.now();
        return reservationMapper.getCancelledReservationCount(userId, now);
    }

    @Override
    public PageResponse<ReservationVo> limitSearch(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Long userId = UserThreadLocal.getUserId();
        Page<ReservationVo> page = reservationMapper.limitSearch(userId, status);
        PageResponse<ReservationVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    @Override
    public List<ReservationRestCountVo> getRestReservationCount(Long time) {
        LocalDateTime localDateTime = LocalDateTimeUtil.of(time); // 将时间戳毫秒值转成LocalDateTime
        return reservationMapper.getRestReservationCount(localDateTime);
    }

    @Override
    public void insertReservation(ReservationDto reservationDto) {
        Long userId = UserThreadLocal.getUserId();
        LocalDateTime now = LocalDateTime.now();
        Integer count = reservationMapper.getCancelledReservationCount(userId, now);
        if (count >= 3) {
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }
        Reservation reservation = BeanUtil.toBean(reservationDto, Reservation.class);
        reservation.setCreateBy(userId);
        reservation.setStatus(ReservationStatus.PENDING.getOrdinal());
        try {
            reservationMapper.insert(reservation);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.TIME_ALREADY_RESERVATED_BY_PHONE);
        }
    }

    @Override
    public void cancelReservation(Long id) {
        reservationMapper.cancelReservation(id);
    }

    @Override
    public void updateExpiredReservations() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneHourAgo = now.minus(1, ChronoUnit.HOURS);
        reservationMapper.updateExpiredReservations(oneHourAgo);
    }
}
